<!--  -->
<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>列表展示</span>
        <el-button
          @click="dialog = true"
          style="float: right; padding: 3px 0"
          type="text"
          >数据添加</el-button
        >
      </div>
      <el-collapse v-model="activeNames">
        <el-collapse-item v-for="(item, index) in datalist" :key="index">
          <template slot="title">
            <span class="title">{{ item.title }}</span>
            <el-button
              @click="deleted"
              class="delete"
              type="primary"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
          <div v-for="(v, i) in item.content" :key="i">
            {{ v }}
          </div>
        </el-collapse-item>
      </el-collapse>
    </el-card>

    <el-drawer
      title="数据添加"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-form-item label="标题" :label-width="formLabelWidth">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="内容" :label-width="formLabelWidth">
            <el-input v-model="form.content" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm">取 消</el-button>
          <el-button type="primary" @click="submit">{{ "确 定" }}</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "",
  data() {
    return {
      activeNames: ["1"],
      table: false,
      dialog: false,
      loading: false,
      form: {
        title: "",
        content: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "80px",
    };
  },
  computed: {
    ...mapState("dataList", ["datalist"]),
  },
  mounted() {
    this.activeNames = this.datalist[0].id;
  },
  methods: {
    deleted() {
      this.$confirm("是否删除此数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "成功删除数据!",
          });
        })
        .catch(() => {});
    },
    submit() {
      this.$confirm("确定要添加数据吗？")
        .then(() => {
          this.dialog = false;
          this.$store.commit('dataList/addData', [0,this.form.title,[this.form.content]])
          this.$message({
            type: "success",
            message: "添加成功!",
          });
        })
        .catch();
    },
    cancelForm() {
      this.dialog = false;
    },
  },
};
</script>

<style scoped>
.el-collapse-item {
  position: relative;
}
.delete {
  z-index: 99;
  position: absolute;
  right: 50px;
}
.demo-drawer__footer {
  text-align: center;
}
</style>